package com.ncu.weatherchina;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.ncu.weatherchina.CityBean.City;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class OneActivity extends AppCompatActivity {
    private ListView listView1;
    private ListView listView2;
    private ListView listView3;
    private String [] province = new String[]{"北京","天津","河北","山西","内蒙古","辽宁","吉林","黑龙江","上海","江苏","浙江","安徽","福建","江西","山东","河南","湖北","湖南","广东","广西","海南","重庆","四川","贵州","云南","西藏","陕西","甘肃","青海","宁夏","新疆","台湾"};
    private String [] city;
    private String [] county;
    private String city_code;
    private String [] w;
    public OneActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        Intent intentGet = getIntent();
        List<City> lstBean = (List<City>) intentGet.getSerializableExtra("list");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(OneActivity.this,R.layout.list,province);
        listView1 = (ListView) findViewById(R.id.list_city_one);
        listView2 = (ListView) findViewById(R.id.list_city_two);
        listView3 = (ListView) findViewById(R.id.list_city_three);

        listView1.setVisibility(View.VISIBLE);
        listView2.setVisibility(View.GONE);
        listView3.setVisibility(View.GONE);
        listView1.setAdapter(adapter);

        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String name = province[position];
                //查询到数据
                List<City> kk = DataSupport.where("province == ?",name).find(City.class);
                city = new String[kk.size()];
                for(int i=0;i<kk.size();i++){
                    city[i] = kk.get(i).getCity();
                }
                Set set = new HashSet();
                //遍历数组并存入集合,如果元素已存在则不会重复存入
                for (int i = 0; i < city.length; i++) {
                    set.add(city[i]);
                }
                Object [] k =  set.toArray();
                w = new String[k.length];
                for(int i=0;i<k.length;i++){
                    w[i]   = (String) k[i];
                }
                ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(OneActivity.this,R.layout.list,w);
                listView2.setAdapter(adapter2);
                listView1.setVisibility(View.GONE);
                listView2.setVisibility(View.VISIBLE);
                listView3.setVisibility(View.GONE);
            }
        });
        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String name = w[position];
                //查询到数据
                List<City> kk = DataSupport.where("city == ?",name).find(City.class);
                county = new String[kk.size()];
                for(int i=0;i<kk.size();i++){
                    county[i] = kk.get(i).getCounty();
                }
                ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(OneActivity.this,R.layout.list, county);
                listView3.setAdapter(adapter3);
                listView1.setVisibility(View.GONE);
                listView2.setVisibility(View.GONE);
                listView3.setVisibility(View.VISIBLE);
            }
        });
        listView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String name = county[position];
                //查询到数据
                List<City> kk = DataSupport.where("county == ?",name).find(City.class);
                city_code = kk.get(0).getCode();
                Log.d("111",city_code);
                Intent intent = new Intent();
                intent.putExtra("data_return",city_code);
                Log.d("111",city_code);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }
    //返回按键回到了activity
    @Override
    public void onBackPressed(){
        Intent intent = new Intent();
        //返回值1，这样就不会更换原来的数据
        intent.putExtra("data_return","1");
        setResult(RESULT_OK,intent);
        finish();
    }

}
